/************************************************************************

								Heroes IV
					  Copyright 2001, The 3DO Company

	------------------------------------------------------------------
  						      random_names.cpp

	$Header: $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#include "random_names.h"

#include <utility>

#include "adv_object_type.h"
#include "elements_of.h"
#include "qualified_adv_object_type.h"
#include "string_insensitive_compare.h"
#include "string_vector.h"
#include "table.h"
#include "table_ptr.h"
#include "town_type.h"

namespace
{
	t_table_cache g_random_names_table( "random_names" );

	// Types of adventure objects that have random names, with their in-table names
	// -1 in m_major_subtype => all objects of m_type
	struct t_type_string
	{
		t_type_string( t_adv_object_type type, int major_subtype, std::string keyword )
			: m_type( type ), m_major_subtype( major_subtype ), m_keyword( keyword )
		{
		}

		t_adv_object_type m_type;
		int               m_major_subtype;
		std::string		  m_keyword;
	};

	static t_type_string const k_randomly_named_types[] =
	{
		t_type_string( k_adv_object_town, k_town_life, "life_town" ),
		t_type_string( k_adv_object_town, k_town_death, "death_town" ),
		t_type_string( k_adv_object_town, k_town_order, "order_town" ),
		t_type_string( k_adv_object_town, k_town_nature, "nature_town" ),
		t_type_string( k_adv_object_town, k_town_chaos, "chaos_town" ),
		t_type_string( k_adv_object_town, k_town_might, "might_town" ),
		t_type_string( k_adv_object_teleporter_entrance, -1,"Teleporter_Entrance" ),
		t_type_string( k_adv_object_teleporter_exit, -1, "Teleporter_Exit" ),
		t_type_string( k_adv_object_ferry, -1, "Ferry" ),
		t_type_string( k_adv_object_gateway, -1, "Gateway" )
	};

	static int const k_type_count = ELEMENTS_OF( k_randomly_named_types );
};

// -------------------------------------------------------------------
// Returns the index of the list of possible random names for the given type of 
// object. Pass index into get_random_name_list() to get actual list of
// random names for type. May return -1 for no list.
// -------------------------------------------------------------------
int get_random_name_list_index( t_qualified_adv_object_type const & type )
{
	// Find type in k_randomly_named_types
	int index = 0;

	while (   index < k_type_count
		   && (   k_randomly_named_types[index].m_type != type.get()
			   || (    k_randomly_named_types[index].m_major_subtype != -1 
				    && k_randomly_named_types[index].m_major_subtype != type.major_subtype() ) ) )
	{
		index++;
	}

	if ( index == k_type_count )
		return -1;
	else
		return index;
}

// -------------------------------------------------------------------
// Returns the index of the list of possible random names for the given type of 
// object. Pass index into get_random_name_list() to get actual list of
// random names for type. May return -1 for no list.
// -------------------------------------------------------------------
t_string_vector const & get_random_name_list( int list_index ) 
{
	static bool initialized = false;
	static t_string_vector random_names[ k_type_count ];

	if ( !initialized )
	{
		initialized = true;

		// Initialize string vectors
		t_table_ptr						table = g_random_names_table.get();
		t_table::iterator				line;

		for (line = table->begin(); line != table->end(); line++)
		{
			int i;
			for ( i = 0; i < k_type_count; i++ )
			{
				if (string_insensitive_compare( (*line)[0], k_randomly_named_types[i].m_keyword ) == 0 )
				{
					random_names[i].push_back( (*line)[1] );
				}
			}
		}
	}

	assert( list_index >= 0 && list_index < k_type_count );

	return random_names[list_index];
}
